import {createRouter, createWebHistory} from 'vue-router'


const routes = [
    {
        path: '/',
        redirect:'/login'

    },
    {
        path: "/login",
        component: () => import('../view/Login/Login')
    },
    {
        name:"admin",
        path: '/admin',
        redirect: '/admin/user',
        component:()=>import('../view/HomePage/HomePage'),
        meta:{
            isLogin:true
        },
        children:[
            {
                path:'user',
                redirect:'/admin/user/list',
                component:()=>import('../view/Admin/Admin'),

                children:[
                    {
                        path:'list',
                        component:()=>import('../view/Admin/AdminList'),
                        meta:{
                            activeMenu:'/admin/user'
                        }
                    },
                    {
                        path:'role',
                        component:()=>import('../view/Admin/AdminControl'),
                        meta:{
                            activeMenu:'/admin/user'
                        }
                    }
                ]

            },
            {
                path: 'goods',
                component:()=>import('../view/Goods/Goods'),
                redirect:'/admin/goods/list',
                children: [
                    {
                        path: 'list',
                        component:()=>import('../view/Goods/GoodsList'),
                        meta:{
                            activeMenu:'/admin/goods'
                        }
                    },
                    {
                        path:'category',
                        component:()=>import('../view/Goods/GoodsCategory'),
                        meta:{
                            activeMenu:'/admin/goods'
                        }
                    },
                    {
                    path: 'create',
                        component:()=>import('../view/Goods/GoodsCreate'),
                        meta: {
                        activeMenu: '/admin/goods'
                        }
                    }
                ]
            }
        ]
    }
]
const router = createRouter({
    routes: routes,
    history: createWebHistory()
})
router.beforeEach((to,from,next)=>{
    const token=sessionStorage.getItem('userToken')
    if(to.meta.isLogin && !token){
        next('/login')
    }else {
        next()
    }
})
export {router}
